package cn.chiship.framework.third.biz.all.service.impl;

import cn.chiship.framework.common.util.FrameworkUtil2;
import cn.chiship.framework.third.core.common.ThirdConstants;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.third.biz.all.mapper.ThirdApplicationKeyConfigMapper;
import cn.chiship.framework.third.biz.all.entity.ThirdApplicationKeyConfig;
import cn.chiship.framework.third.biz.all.entity.ThirdApplicationKeyConfigExample;
import cn.chiship.framework.third.biz.all.service.ThirdApplicationKeyConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 三方应用集成配置业务接口实现层
 * 2022/6/12
 *
 * @author lijian
 */
@Service
public class ThirdApplicationKeyConfigServiceImpl extends BaseServiceImpl<ThirdApplicationKeyConfig, ThirdApplicationKeyConfigExample> implements ThirdApplicationKeyConfigService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdApplicationKeyConfigServiceImpl.class);

    @Resource
    ThirdApplicationKeyConfigMapper thirdApplicationKeyConfigMapper;

    @Override
    public BaseResult insertSelective(ThirdApplicationKeyConfig thirdApplicationKeyConfig) {
        thirdApplicationKeyConfig.setProjectId(FrameworkUtil2.getGlobalProperties().getId());
        ThirdApplicationKeyConfigExample applicationKeyConfigExample = new ThirdApplicationKeyConfigExample();
        applicationKeyConfigExample.createCriteria()
                .andProjectIdEqualTo(FrameworkUtil2.getGlobalProperties().getId())
                .andAppIdEqualTo(thirdApplicationKeyConfig.getAppId());
        if (thirdApplicationKeyConfigMapper.countByExample(applicationKeyConfigExample) > 0) {
            return BaseResult.error("appId已存在,请重新输入！");
        }
        return super.insertSelective(thirdApplicationKeyConfig);
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(ThirdApplicationKeyConfig thirdApplicationKeyConfig) {
        thirdApplicationKeyConfig.setProjectId(FrameworkUtil2.getGlobalProperties().getId());
        ThirdApplicationKeyConfigExample applicationKeyConfigExample = new ThirdApplicationKeyConfigExample();
        applicationKeyConfigExample.createCriteria()
                .andProjectIdEqualTo(FrameworkUtil2.getGlobalProperties().getId())
                .andAppIdEqualTo(thirdApplicationKeyConfig.getAppId())
                .andIdNotEqualTo(thirdApplicationKeyConfig.getId());
        if (thirdApplicationKeyConfigMapper.countByExample(applicationKeyConfigExample) > 0) {
            return BaseResult.error("appId已存在,请重新输入！");
        }
        return super.updateByPrimaryKeySelective(thirdApplicationKeyConfig);
    }

    @Override
    public BaseResult initConfig(Byte type) {
        ThirdApplicationKeyConfigExample applicationKeyConfigExample = new ThirdApplicationKeyConfigExample();
        applicationKeyConfigExample.createCriteria().andProjectIdEqualTo(FrameworkUtil2.getGlobalProperties().getId()).andTypeEqualTo(type);
        List<ThirdApplicationKeyConfig> applicationKeyConfigs = thirdApplicationKeyConfigMapper.selectByExample(applicationKeyConfigExample);
        if (applicationKeyConfigs.isEmpty()) {
            return BaseResult.ok(null);
        }
        return BaseResult.ok(applicationKeyConfigs.get(0));
    }

    @Override
    public BaseResult config(ThirdApplicationKeyConfig applicationKeyConfig) {
        ThirdApplicationKeyConfigExample applicationKeyConfigExample = new ThirdApplicationKeyConfigExample();
        applicationKeyConfigExample.createCriteria().andIsDeletedEqualTo(BaseConstants.NO).andProjectIdEqualTo(FrameworkUtil2.getGlobalProperties().getId()).andTypeEqualTo(applicationKeyConfig.getType());
        List<ThirdApplicationKeyConfig> applicationKeyConfigs = thirdApplicationKeyConfigMapper.selectByExample(applicationKeyConfigExample);
        if (applicationKeyConfigs.isEmpty()) {
            applicationKeyConfig.setProjectId(FrameworkUtil2.getGlobalProperties().getId());
            super.insertSelective(applicationKeyConfig);
            return BaseResult.ok();
        }
        applicationKeyConfig.setId(applicationKeyConfigs.get(0).getId());
        super.updateByPrimaryKeySelective(applicationKeyConfig);
        return BaseResult.ok();
    }
}
